package ro.flashbite.helpers
{
	import ro.flashbite.errors.ArgumentIncompatibleError;
	import ro.flashbite.errors.DontInstanciateError;
	
	import fl.controls.Button;
	import fl.controls.UIScrollBar;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * Static helpers for drawing primitives, also for flash components with specific properties
	 * 
	 * v 1.0
	 * 
	 * @author Adrian Barbu
	 */	
	public final class DrawingHelpers
	{
		public function DrawingHelpers() { throw new DontInstanciateError(); }
		
		/**
		 * Draw the bounding box of a sprite in his graphics
		 *  
		 * @param sprite : (Sprite) sprite to draw bounds on
		 */		
		public static function drawBounds(sprite:Sprite):void
		{
			if (sprite == null) {
				throw new ArgumentIncompatibleError("sprite");
			}
			
			const bounds:Rectangle = sprite.getBounds(null);
			
			sprite.graphics.endFill();
			sprite.graphics.lineStyle(1);
			sprite.graphics.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
		}
		
		/**
		 * Draw a X like point in a sprite graphics
		 *  
		 * @param sprite : (Sprite) sprite used to draw point
		 * @param pt : (Point) location in sprite coordonates were the point will be drawed
		 */		
		public static function drawPoint(sprite:Sprite, pt:Point):void
		{
			if (sprite == null) {
				throw new ArgumentIncompatibleError("displayObject");
			}
			if (pt == null) {
				throw new ArgumentIncompatibleError("pt");
			}
			
			var g:Graphics = sprite.graphics; 
			
			g.lineStyle(2, 0xFF0000);
			g.moveTo(pt.x - 2, pt.y + 2);
			g.lineTo(pt.x + 2, pt.y - 2);
			g.moveTo(pt.x - 2, pt.y - 2);
			g.lineTo(pt.x + 2, pt.y + 2);
		}
		
		/**
		 * Create a basic button with specific properties
		 * 
		 * @param w : (Number) desired button width
		 * @param h : (Number) desired button height
		 * @param tFormat : (TextFormat) desired text format of button label/name
		 * @param name : (String) name of the button
		 * @param moveTo : (Point) location as x and y of the created button
		 * @param parentMc : (Sprite) the sprite were to add as a child this button
		 * @return : (Button) a new instance of button, with specific properties
		 */		
		public static function createBasicButton(w:Number, h:Number, tFormat:TextFormat, name:String, moveTo:Point = null, parentMc:Sprite = null):Button
		{
			var button:Button = new Button();
			
			button.buttonMode = true;
			button.tabEnabled = false;
			button.width = w;
			button.height = h;
			
			if (name) {
				button.name = name;
				button.label = /*StringHelpers.capitalizeFirstLetter(*/name/*)*/;
				button.textField.autoSize = TextFieldAutoSize.CENTER;
			}
			
			if (parentMc)
				parentMc.addChild(button);
			
			if (moveTo) {
				button.x = moveTo.x;
				button.y = moveTo.y;
			}			
			
			return button;
		}
		
		/**
		 * Create a basic textField with simple default properties: location, non-selectable, default given dimesions
		 * 
		 * @param w : (Number) desired textField width
		 * @param h : (Number) desired textField height
		 * @param tFormat : (TextFormat) desired text format of textField
		 * @param name : (String) name of the textField
		 * @param text : (String) text to add on textField
		 * @param moveTo : (Point) location as x and y of the created textField
		 * @param parentMc : (Sprite) the sprite were to add as a child this textField
		 * @return  : (TextField) a new instance of textField that can act as a TextArea
		 */			 
		static public function createBasicTextField(w:Number, h:Number, tFormat:TextFormat, name:String, text:String, moveTo:Point = null, parentMc:Sprite = null):TextField
		{
			var tf:TextField = new TextField();
			
			tf.tabEnabled = false;
			tf.selectable = false;
			
			if (text) {
				tf.text = text;
			}
			
			if (tFormat) {
				tf.setTextFormat(tFormat);
			}
			
			if (name) {
				tf.name = name;
			}
			
			if (parentMc) {
				parentMc.addChild(tf);
			}
			
			if (moveTo) {
				tf.x = moveTo.x;
				tf.y = moveTo.y;
			}	
			
			if (w && h) {
				tf.width = w;
				tf.height = h;
			} else {
				tf.autoSize = TextFieldAutoSize.LEFT;
			}
			
			return tf;
		}
		
		/**
		 * Create a basic textField with properties like TextArea : multiline, wordWrap, etc...
		 * 
		 * @param w : (Number) desired textField width
		 * @param h : (Number) desired textField height
		 * @param tFormat : (TextFormat) desired text format of textField
		 * @param name : (String) name of the textField
		 * @param text : (String) text to add on textField
		 * @param moveTo : (Point) location as x and y of the created textField
		 * @param parentMc : (Sprite) the sprite were to add as a child this textField
		 * @return : (TextField) a new instance of textField that can act as a TextArea
		 */		
		public static function createTextFieldLikeTextArea(w:Number, h:Number, tFormat:TextFormat, name:String, text:String, moveTo:Point = null, parentMc:Sprite = null):TextField
		{
			var tf:TextField = createBasicTextField(w, h, tFormat, name, text, moveTo, parentMc)
			
			tf.multiline = true;
			tf.wordWrap = true;
			tf.border = true;
			tf.background = true;
			tf.backgroundColor = 0xFFFFFF;
			
			return tf;
		}
		
		/**
		 * Create a basic UIScrollBar and attach to a textField
		 * 
		 * @param tf : (TextField) the textField to act as a target for UIScrollBar
		 * @param parentMc : (Sprite) the sprite were to add as a child this textField
		 * @return : (UIScrollBar) a new instace of UIScrollBar attached to a textField
		 */		
		public static function createScrollBarAndAttachToTextField(tf:TextField, parentMc:Sprite = null):UIScrollBar
		{
			var bar:UIScrollBar = new UIScrollBar();
			
			bar.tabEnabled = false;
			bar.scrollTarget = tf;
			bar.mouseEnabled = true;
			bar.enabled = true;
			
			bar.height = tf.height;
			bar.x = tf.x + tf.width;
			bar.y = tf.y;
			
			if (parentMc)
				parentMc.addChild(bar);
			
			return bar;
		}
	}
}